package zpf.zpf;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.util.concurrent.ArrayBlockingQueue;
@Slf4j
@SpringBootApplication
public class ZpfApplication {

    public static ArrayBlockingQueue<String> queue=new ArrayBlockingQueue<String>(100);
    @SneakyThrows
    public static void main(String[] args) {
      new Thread(() -> {
          System.setProperty("java.awt.headless", "false");
          SpringApplicationBuilder builder = new SpringApplicationBuilder(ZpfApplication.class);
          builder.headless(false).run(args);
      }).start();
      //经过测试，模拟的按键精灵 只能在主线程下才能正确执行，这个限制和操作系统有关系
      //执行这个 需要加启动参数 例如 java -jar -XstartOnFirstThread   应用.jar
      while (queue.take()!=null){
          //声明一个StingSelection 对象，并使用String的参数完成实例化；
          StringSelection stringSelection = new StringSelection("我是机器人-allSchool-推广文案..来自按键精灵的复制");
          //使用Toolkit对象的setContents将字符串放到粘贴板中 ；
          Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
          Robot robot = new Robot() ;
          robot.setAutoDelay(1);
          //清空输入框的内容
          for(int i=0;i<200;i++){
              robot.keyPress(KeyEvent.VK_BACK_SPACE);
          }
          robot.keyRelease(KeyEvent.VK_BACK_SPACE);
          robot.setAutoDelay(50);
          //按下command v键 ；
          robot.keyPress(KeyEvent.VK_META);
          robot.keyPress(KeyEvent.VK_V);
          //command v 键
          robot.keyRelease(KeyEvent.VK_V);
          robot.keyRelease(KeyEvent.VK_META);
      }

    }

}
